Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persist execution context in storage target #359

Merged
merged 14 commits into from
Jun 6, 2022

Conversation

cisaacstern
Copy link
Member

This is a WIP to address @rabernat's suggestion in #37 (comment) that we persist execution context information in the storage target, which is the first part of a solution for #37 and pangeo-forge/user-stories#5.

Here's a rough idea of how this might look:

  • BaseRecipe.get_execution_context compiles a dict of metadata we want to capture
  • Each recipe class individually elects when/where this will be persisted to the target. The stage in which this occurs will be specific to each recipe class (finalize_target for XarrayZarrRecipe, a different stage for HDFReferenceRecipe, etc.). As will the exact location in the target (.zmetadata for XarrayZarrRecipe, somewhere else for other storage formats, etc.).

@cisaacstern
Copy link
Member Author

The latest test failures are from test_XarrayZarrRecipe.py::test_process, and stem from the fact that in #349 we punted on how to hash preprocessor functions. In this PR, we're calling recipe.get_execution_context() (which includes a recipe.sha256() call) on every execution, so the tests which include preprocessors fail. The options would seem to be:

  1. Find a way to deterministically hash Python functions. (I've previously wondered if the function objects __code__ attribute might offer some path forward here...)
  2. Don't set a "pangeo-forge:recipe_hash" if the instance contains a preprocessor.
  3. Do set a "pangeo-forge:recipe_hash" for recipes with a preprocessor, but exclude the preprocessor from the calculation. We might think of this as thematically similar to the unsafe_hash option for built-in dataclasses.

Preprocessors are a very useful feature, as (will be) hashes, so my preference would be to try to find a way to achieve option 1 if possible. I will take a brief pause on this PR to look into this, but if this seems infeasible, as a backup plan we could move forward with option 2 (not setting hashes for recipes with preprocessors). It seems to me that option 3 could lead to some very confusing situations which we'd prefer to avoid.

Copy link
Contributor

@rabernat rabernat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking great! Just one nit pick about the testing syntax.

tests/recipe_tests/test_XarrayZarrRecipe.py Outdated Show resolved Hide resolved
docs/pangeo_forge_recipes/recipe_user_guide/execution.md Outdated Show resolved Hide resolved
@rabernat
Copy link
Contributor

Find a way to deterministically hash Python functions. (I've previously wondered if the function objects code attribute might offer some path forward here...)

Let's use inspect for this.

import inspect

def foo(a, b):
    a += 1
    return a + b

inspect.getsource(foo)

I think that is a fine solution for now. It's a little bit brittle because it is sensitive to changes in formatting of the function. However, it should be sufficient to detect when a defined function has not changed at all.

I was not able to get __code__.co_code to work, as in this example

def foo(a, b):
    a += 1
    return a + b

def bar(a, b):
    a += 2
    return a + b

# this assertion fails
assert foo.__code__.co_code != bar.__code__.co_code

@cisaacstern
Copy link
Member Author

Thanks for the review, Ryan. inspect.getsource is very cool, hadn't seen it before! I think that's the last thing we need here, will add that now.

Copy link
Contributor

@rabernat rabernat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I never merged this! I just see one small issue, then LGTM.

def drop_execution_context_attrs(ds: xr.Dataset) -> xr.Dataset:
"""Drop pangeo-forge execution context attrs from a dataset."""

ds_copy = ds.copy(deep=True)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need a deep copy here? That will use a lot more memory.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure! It's "just" the tests so happy to use regular copy and see how it goes.

TBH, I've now run into enough weird behavior of copied dictionaries that are not deep copies (due to shared references with the original, I guess), that I just reflexively make all dictionary copies deep now. 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants